package com.dd.edu.rabbitmqboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 定义订单的队列、交换机、队列绑定交换机、设置路由key
 */
@Configuration
public class OrderRabbitConfig {

    @Bean
    public Queue orderQueueTTL(){
        Map<String, Object> arguments = new HashMap<>();
        // ttl = 30s
        arguments.put("x-message-ttl", 30 * 1000);
        // 设置关联的死信队列
        if (true) {
            arguments.put("x-dead-letter-exchange", "ex.order.dlx");
            arguments.put("x-dead-letter-routing-key", "order.pay.dlx");
        }

        return new Queue("q.order.ttl", false, false, false, arguments);
    }

    @Bean
    public Exchange orderExchangeTTL() {
        return new DirectExchange("ex.order.ttl", false, false);
    }
    @Bean
    public Binding orderBindingTTL(){
        return BindingBuilder.bind(orderQueueTTL()).to(orderExchangeTTL()).with("order.pay.ttl").noargs();
    }
}
